﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.IO;
using SmartXLS;
using System.Drawing;
using UygulamaTakbisService.wsTakbisServis;

namespace UygulamaTakbisService
{
    public class BBYaz
    {
        public byte[] Yaz(List<BagimsizBolumBilgi> bagimsizBolumBilgiList, string serverKayitYolu)
        {
            WorkBook workBook = new WorkBook();
            workBook.setSheetName(0, "Bag Bol Listesi");

            int satir = 0;
            int sutun = 0;

            workBook.setText(satir, sutun, "Kat");
            sutun++;
            workBook.setText(satir, sutun, "Blok");
            sutun++;
            workBook.setText(satir, sutun, "Giris");
            sutun++;
            workBook.setText(satir, sutun, "Bagımsız BölümNo");
            sutun++;
            workBook.setText(satir, sutun, "KmKiTip");
            sutun++;
            workBook.setText(satir, sutun, "Aktif Mi");
            sutun++;
            workBook.setText(satir, sutun, "Tesis İşlem Adı");
            sutun++;
            workBook.setText(satir, sutun, "Tesis Yevmiye No");
            sutun++;
            workBook.setText(satir, sutun, "Tesis Yevmiye Tarih");
            sutun++;
            workBook.setText(satir, sutun, "Tesis Yevmiye Kurum");
            sutun++;
            workBook.setText(satir, sutun, "Terkin Tanım Adı");
            sutun++;
            workBook.setText(satir, sutun, "Terkin Yevmiye No");
            sutun++;
            workBook.setText(satir, sutun, "Terkin Yevmiye Tarih");
            sutun++;
            workBook.setText(satir, sutun, "Terkin Yevmiye Kurum");
            sutun++;

            RangeStyle rangeStyleBaslik = workBook.getRangeStyle(0, 0, 0, sutun);
            rangeStyleBaslik.FontBold = true;
            rangeStyleBaslik.FontColor = Color.DarkRed.ToArgb();
            rangeStyleBaslik.HorizontalAlignment = RangeStyle.HorizontalAlignmentCenter;
            rangeStyleBaslik.VerticalAlignment = RangeStyle.VerticalAlignmentCenter;
            rangeStyleBaslik.LeftBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.RightBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.TopBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.BottomBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.VerticalInsideBorder = RangeStyle.BorderThin;
            workBook.setRangeStyle(rangeStyleBaslik, 0, 0, 0, sutun - 1);

            string deger = string.Empty;
            foreach (BagimsizBolumBilgi bagimsizBolumBilgi in bagimsizBolumBilgiList)
            {
                satir++;
                sutun = 0;

                if (bagimsizBolumBilgi.Kat != null)
                    deger = bagimsizBolumBilgi.Kat.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (bagimsizBolumBilgi.Blok != null)
                    deger = bagimsizBolumBilgi.Blok.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (bagimsizBolumBilgi.Giris != null)
                    deger = bagimsizBolumBilgi.Giris.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (bagimsizBolumBilgi.BagimsizBolumNo != null)
                    deger = bagimsizBolumBilgi.BagimsizBolumNo.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (bagimsizBolumBilgi.KMKITip != null)
                    deger = bagimsizBolumBilgi.KMKITip.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (bagimsizBolumBilgi.TapuBolumDurum != null)
                {
                    if (bagimsizBolumBilgi.TapuBolumDurum != null)
                        deger = bagimsizBolumBilgi.TapuBolumDurum.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (bagimsizBolumBilgi.TesisIslem != null)
                {
                    if (bagimsizBolumBilgi.TesisIslem.IslemTanimAd != null)
                        deger = bagimsizBolumBilgi.TesisIslem.IslemTanimAd.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (bagimsizBolumBilgi.TesisIslem != null && bagimsizBolumBilgi.TesisIslem.Yevmiye != null)
                {
                    if (bagimsizBolumBilgi.TesisIslem.Yevmiye.YevmiyeNo != null)
                        deger = bagimsizBolumBilgi.TesisIslem.Yevmiye.YevmiyeNo.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (bagimsizBolumBilgi.TesisIslem != null && bagimsizBolumBilgi.TesisIslem.Yevmiye != null)
                {
                    if (bagimsizBolumBilgi.TesisIslem.Yevmiye.YevmiyeNo != null)
                        deger = bagimsizBolumBilgi.TesisIslem.Yevmiye.Tarih.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (bagimsizBolumBilgi.TesisIslem != null && bagimsizBolumBilgi.TesisIslem.Yevmiye != null)
                {
                    if (bagimsizBolumBilgi.TesisIslem.Yevmiye.Kurum != null)
                        deger = bagimsizBolumBilgi.TesisIslem.Yevmiye.Kurum.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (bagimsizBolumBilgi.TerkinIslem != null)
                {
                    if (bagimsizBolumBilgi.TerkinIslem.IslemTanimAd != null)
                        deger = bagimsizBolumBilgi.TerkinIslem.IslemTanimAd.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (bagimsizBolumBilgi.TerkinIslem != null && bagimsizBolumBilgi.TerkinIslem.Yevmiye != null)
                {
                    if (bagimsizBolumBilgi.TerkinIslem.Yevmiye.YevmiyeNo != null)
                        deger = bagimsizBolumBilgi.TerkinIslem.Yevmiye.YevmiyeNo.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (bagimsizBolumBilgi.TerkinIslem != null && bagimsizBolumBilgi.TerkinIslem.Yevmiye != null)
                {
                    if (bagimsizBolumBilgi.TerkinIslem.Yevmiye.Tarih != null)
                        deger = bagimsizBolumBilgi.TerkinIslem.Yevmiye.Tarih.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (bagimsizBolumBilgi.TerkinIslem != null && bagimsizBolumBilgi.TerkinIslem.Yevmiye != null)
                {
                    if (bagimsizBolumBilgi.TerkinIslem.Yevmiye.Kurum != null)
                        deger = bagimsizBolumBilgi.TerkinIslem.Yevmiye.Kurum.ToString();
                }
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                RangeStyle rangeStyleVeri = workBook.getRangeStyle(satir, 0, satir, sutun);
                rangeStyleVeri.HorizontalAlignment = RangeStyle.HorizontalAlignmentLeft;
                rangeStyleVeri.VerticalAlignment = RangeStyle.VerticalAlignmentCenter;
                rangeStyleVeri.LeftBorder = RangeStyle.BorderThin;
                rangeStyleVeri.RightBorder = RangeStyle.BorderThin;
                rangeStyleVeri.TopBorder = RangeStyle.BorderThin;
                rangeStyleVeri.BottomBorder = RangeStyle.BorderThin;
                rangeStyleVeri.VerticalInsideBorder = RangeStyle.BorderThin;
                workBook.setRangeStyle(rangeStyleVeri, satir, 0, satir, sutun - 1);
            }

            for (int i = 0; i < sutun + 1; i++)
                workBook.setColWidthAutoSize(i, true);

            workBook.write(serverKayitYolu);
            workBook.Dispose();

            GC.GetTotalMemory(false);
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.GetTotalMemory(true);

            return File.ReadAllBytes(serverKayitYolu);
        }
    }
}
